/*
 * Copyright (C) 2011, EADS France
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */
package xowl.gmi.view;

/**
 *
 * @author Laurent WOUTERS
 */
public class ToolboxElement extends javax.swing.JPanel {
    protected int padValue;
    protected java.awt.Color hoverColor;
    protected boolean isHovered;
    protected javax.swing.JLabel label;
    
    public ToolboxElement(String description, ViewStyle style) {
        this.padValue = style.toolboxPadSize;
        this.hoverColor = style.toolboxHoverColor;
        
        label = new javax.swing.JLabel(description);
        label.setOpaque(false);
        label.setFont(new java.awt.Font(style.fontName, style.fontStyle, style.fontSize));
        label.setHorizontalAlignment(javax.swing.JLabel.CENTER);
        label.setVerticalAlignment(javax.swing.JLabel.CENTER);
        
        javax.swing.JPanel row = new javax.swing.JPanel();
        row.setOpaque(false);
        row.setLayout(new javax.swing.BoxLayout(row, javax.swing.BoxLayout.X_AXIS));
        row.setAlignmentX(java.awt.Component.CENTER_ALIGNMENT);
        row.add(new Filler(padValue, 1));
        row.add(label);
        row.add(new Filler(padValue, 1));

        this.setOpaque(false);
        this.setLayout(new javax.swing.BoxLayout(this, javax.swing.BoxLayout.Y_AXIS));
        this.add(new Filler(1, padValue));
        this.add(row);
        this.add(new Filler(1, padValue));
        
        this.setMaximumSize(new java.awt.Dimension(Integer.MAX_VALUE, 30));
        this.setPreferredSize(new java.awt.Dimension(75, 30));
        this.setMinimumSize(new java.awt.Dimension(30, 30));
        
        java.awt.event.MouseListener ml = new java.awt.event.MouseListener() {
            @Override public void mouseClicked(java.awt.event.MouseEvent e) { }
            @Override public void mousePressed(java.awt.event.MouseEvent e) { }
            @Override public void mouseReleased(java.awt.event.MouseEvent e) { }
            @Override public void mouseEntered(java.awt.event.MouseEvent e) {
                isHovered = true;
                ToolboxElement.this.repaint();
            }
            @Override public void mouseExited(java.awt.event.MouseEvent e) {
                isHovered = false;
                ToolboxElement.this.repaint();
            }
        };
        this.addMouseListener(ml);
        label.addMouseListener(ml);
    }
    
    
    protected boolean mustPaint() { return (isHovered); }
    protected java.awt.Color getPaintColor() {
        if (isHovered) return hoverColor;
        else return null;
    }
    
    @Override public void paintComponent(java.awt.Graphics g) {
        if (mustPaint()) {
            java.awt.Rectangle bounds = this.getBounds();
            java.awt.Graphics2D g2d = (java.awt.Graphics2D)g.create();
            g2d.setColor(getPaintColor());
            g2d.fillRect(0, 0, bounds.width, bounds.height);
        }
    }
}
